#include <server.h>
#include "cli.h"
#include <iostream>
#include "json.h"
#include "common.h"


unsigned int port = 4096;

int main(int argc, char *argv[]) {
	// 解析命令行参数到 CliParam 封装类
    CliParam cliParam = GetOptLong(argc,argv);
	if(cliParam.webPort > 0){
		port = cliParam.webPort;
	}

	std::string devConfiPath = cliParam.devConfigPath;
	std::string deviceStr;
	if (!ReadFileContent(devConfiPath.c_str(), std::ios_base::binary, deviceStr))
	{
		std::cerr <<"read device config error, path is:" << devConfiPath <<std::endl;
	}
	// 从json文件解析设备信息到 ProtocolMessage
	ProtocolMessage protocalMsg = ProtocolMessage::parseMessage(TrimUTF8BOM(deviceStr));          
    server Simulator_Server;
	try {
        on_run(Simulator_Server, port);
	}
	catch (websocketpp::exception const& e) {
		std::cout << e.what() << std::endl;
	}
	catch (const std::exception& e) {
		std::cout << e.what() << std::endl;
	}
	catch (...) {
		std::cout << "other exception" << std::endl;
	}

    return 0;
}